package com.smedi.ismedi.inmail.core.application.service.impl;

import com.smedi.ismedi.authority.starter.annotation.SkipAuthority;
import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.inmail.core.application.service.ScheduleApplicationService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.TaskRecord;
import com.smedi.ismedi.inmail.core.domain.service.TaskDomainService;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.TaskRecordMapper;
import com.smedi.ismedi.notification.api.dto.request.SendSmsRequest;
import com.smedi.ismedi.notification.api.feign.RemoteSmsNotificationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ScheduleApplicationServiceImpl implements ScheduleApplicationService {
    private final TaskDomainService taskDomainService;
    private final RemoteSmsNotificationFeignApi remoteSmsNotificationFeignApi;

    private final TaskRecordMapper taskRecordMapper;


    @Override
    //@Transactional(rollbackFor = Exception.class)
    @SkipAuthority
    @SkipSqlPermission
    public void doSync() {
        List<TaskRecord> taskRecordList = new ArrayList<>();
        String typeOne = "隐患整改";
        String typeTwo = "隐患退回整改";
        String typeTree = "检查整改";
        String status = "已逾期";
        if (taskDomainService.queryTaskByTypeAndStatus(typeOne, status) != null) {
            taskRecordList.addAll(taskDomainService.queryTaskByTypeAndStatus(typeOne, status));
        }
        if (taskDomainService.queryTaskByTypeAndStatus(typeTwo, status) != null) {
            taskRecordList.addAll(taskDomainService.queryTaskByTypeAndStatus(typeTwo, status));
        }
        if (taskDomainService.queryTaskByTypeAndStatus(typeTree, status) != null) {
            taskRecordList.addAll(taskDomainService.queryTaskByTypeAndStatus(typeTree, status));
        }

        for (TaskRecord taskRecord : taskRecordList) {
            if (taskRecord != null) {
                SendSmsRequest dealSendSmsRequest = new SendSmsRequest();
                List<String> dealReceiverPhone = new ArrayList<>();
                List<String> dealParameters = new ArrayList<>();

                //receiverPhone.add(remotePersonCoreFeignApi.getQueryPersonInfo(taskRecord.getDealPersonId()).getData().getMobile());
                dealReceiverPhone.add("18616256383");
                //dealReceiverPhone.add("18616256383");
                dealParameters.add(taskRecord.getDealPersonName() != null ? taskRecord.getDealPersonName() : null);
                dealParameters.add(taskRecord.getProjectName() != null ? taskRecord.getProjectName() : null);
                LocalDate now = LocalDate.now();
                if (taskRecord.getExpireTime() != null) {
                    LocalDate localDate = taskRecord.getExpireTime().toLocalDate();
                    int days = (int) ((now.getDayOfYear() - localDate.getDayOfYear()));
                    //Duration duration = Duration.between(localDate, now);
                    //Long days = duration.toDays(); //相差的天数
                    dealParameters.add(String.valueOf(days));
                } else {
                    dealParameters.add(null);
                }
                dealParameters.add(taskRecord.getTaskName());
                dealParameters.add(taskRecord.getTaskDescribe());

                dealSendSmsRequest.setSmsTemplateId(2L)
                        .setReceivers(dealReceiverPhone)
                        .setParameters(dealParameters);
                ResponseData<Boolean> booleanResponseData = remoteSmsNotificationFeignApi.postSendShortMessage(dealSendSmsRequest);
                log.info("定时任务是否发送" + booleanResponseData.getData());
            }
        }
    }

    @Override
    public int doTaskSync() {
        return taskRecordMapper.updateTaskStatus();
    }
}
